package com.cyjt.operation.ui;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;
import com.cyjt.operation.R;
import com.cyjt.operation.base.AppContext;
import com.cyjt.operation.base.HandlerMessageCodes;
import com.cyjt.operation.bean.DeviceStatus;
import com.cyjt.operation.view.ActionBarLayout;

/**
 * 设备状态的Activity
 * 
 * @author LTP
 * 
 */
public class DeviceStatusActivity extends Activity implements OnClickListener {
	/** 3G路由功能键 */
	private ImageView imageView_device_status_routing;
	/** 3G路由状态提示 */
	private ImageView imageView_device_status_routing_status;
	/** 基站功能键 */
	private ImageView imageView_device_status_basestation;
	/** 基站状态提示 */
	private ImageView imageView_device_status_basestation_status;
	/** 节点功能键 */
	private ImageView imageView_device_status_sensor;
	/** 节点状态提示 */
	private ImageView imageView_device_status_sensor_status;
	private DeviceStatus deviceStatus;
	@SuppressLint("HandlerLeak")
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case HandlerMessageCodes.FETCH_DEVICE_STATUS_SUCCEED:
				// 获取设备状态成功
				ActionBarLayout.isShowProgressBar(false);
				deviceStatus = (DeviceStatus) msg.obj;
				updateDeviceStatusView();// 更新设备状态的显示（若有异常显示红色警告）
				break;
			case HandlerMessageCodes.FETCH_DEVICE_STATUS_FAILED:
				break;
			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_device_status);
		initView();
		// 从服务器获取设备状态
		AppContext.getInstance().getVolleyTools()
				.fetchDeviceStatus(handler, AppContext.getInstance().getUser());
	}

	/**
	 * 初始化控件方法
	 */
	private void initView() {
		ActionBarLayout.setTitle("设备状态");
		ActionBarLayout.isShowProgressBar(true);
		imageView_device_status_routing = (ImageView) findViewById(R.id.imageView_device_status_routing);
		imageView_device_status_routing.setOnClickListener(this);
		imageView_device_status_routing_status = (ImageView) findViewById(R.id.imageView_device_status_routing_status);
		imageView_device_status_basestation = (ImageView) findViewById(R.id.imageView_device_status_basestation);
		imageView_device_status_basestation.setOnClickListener(this);
		imageView_device_status_basestation_status = (ImageView) findViewById(R.id.imageView_device_status_basestation_status);
		imageView_device_status_sensor = (ImageView) findViewById(R.id.imageView_device_status_sensor);
		imageView_device_status_sensor.setOnClickListener(this);
		imageView_device_status_sensor_status = (ImageView) findViewById(R.id.imageView_device_status_sensor_status);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.imageView_device_status_routing:
			// 跳转到3G路由列表的Activity
			startActivity(new Intent(this, RoutingListActivity.class));
			break;
		case R.id.imageView_device_status_sensor:
			// 跳转到节点列表的Activity
			startActivity(new Intent(this, NodesStatusListActivity.class));
			break;
		case R.id.imageView_device_status_basestation:
			// 跳转到基站列表的Activity
			startActivity(new Intent(this, BaseStationStatusListActivity.class));
			break;
		default:
			break;
		}
	}

	/**
	 * 更新设备状态的显示（若有异常显示红色警告）
	 */
	private void updateDeviceStatusView() {
		if (deviceStatus.getSimCardStatus() == 2
				|| deviceStatus.getNodeStatus() == 2
				|| deviceStatus.getBaseStationStatus() == 2) {
			Toast.makeText(this, "设备有异常，异常的设备会显示红色感叹号", Toast.LENGTH_SHORT)
					.show();
		}
		if (deviceStatus.getSimCardStatus() == 2) {
			imageView_device_status_routing_status
					.setImageResource(R.drawable.danger);
		}
		if (deviceStatus.getNodeStatus() == 2) {
			imageView_device_status_sensor_status
					.setImageResource(R.drawable.danger);
		}
		if (deviceStatus.getBaseStationStatus() == 2) {
			imageView_device_status_basestation_status
					.setImageResource(R.drawable.danger);
		}
	};
}
